﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using SpectrumAnalyser.Common.Logs;
using SpectrumAnalyser.Common.Logs.Attributes;
using SpectrumAnalyser.ModelObjects;
using SpectrumAnalyser.Shell.Constants;
using SpectrumAnalyser.Shell.ViewModels;
using SpectrumAnalyser.Shell.ViewModels.Configuration;
using SpectrumDbConnection = SpectrumAnalyser.ModelObjects.SpectrumDbConnection;

namespace SpectrumAnalyser.Shell.Helpers
{
    [Log]
    public interface IAppConfigHelper
    {
        void UpdateFileHistory(IList<SpectrumFileGroup> files);
        IList<SpectrumFileGroup> GetFilesHistory();

        void UpdateSpectrumDbConnection(IList<SpectrumDbConnection> spectrumDbConnections);
        IList<SpectrumDbConnection> GetSpectrumDbConnection();

        void UpdatePeaksSearchSettings(PeakSearchSettings searchSettings);
        PeakSearchSettings GetPeaksSearchSettings();

        void UpdateLinesSearchSettings(LinesSearchSettings linesSearchSettings);
        LinesSearchSettings GetLinesSearchSettings();
    }

    public class AppConfigHelper : IAppConfigHelper
    {
        private readonly AppConfig _appConfig = new AppConfig();

        public AppConfigHelper()
        {
            var fileInfo = new FileInfo(FilesConst.APP_CONFIG_FILE);
            if (fileInfo.Exists)
            {
                var stream = new FileStream(fileInfo.FullName, FileMode.Open);
                var bf = new BinaryFormatter();

                try
                {
                    _appConfig = (AppConfig)bf.Deserialize(stream);
                }
                catch (Exception e)
                {
                    Logger.Log(e.Message);
                    _appConfig = new AppConfig();
                }
                stream.Close();
            }
            else
            {
                fileInfo.Create();
                _appConfig = new AppConfig();
            }
        }


        public void UpdateFileHistory(IList<SpectrumFileGroup> files)
        {
            _appConfig.FilesHistory = files.ToList();
            SaveConfig();
        }

        public IList<SpectrumFileGroup> GetFilesHistory()
        {
            return _appConfig.FilesHistory ?? new List<SpectrumFileGroup>();
        }


        public void UpdateSpectrumDbConnection(IList<SpectrumDbConnection> spectrumDbConnections)
        {
            var result = new List<SpectrumDbConnection>();
            result.AddRange(spectrumDbConnections.Select(connection => connection));
            _appConfig.SpectrumConnections = result;
            SaveConfig();
        }

        public IList<SpectrumDbConnection> GetSpectrumDbConnection()
        {
            var connections = new List<SpectrumDbConnection>();
            if (!ReferenceEquals(_appConfig.SpectrumConnections, null))
            {
                connections.AddRange(_appConfig.SpectrumConnections);
            }
            return connections.ToList();
        }


        public void UpdatePeaksSearchSettings(PeakSearchSettings searchSettings)
        {
            _appConfig.SearchSettings = searchSettings;
            SaveConfig();
        }

        public PeakSearchSettings GetPeaksSearchSettings()
        {
            return _appConfig.SearchSettings ?? new PeakSearchSettings();
        }

        public void UpdateLinesSearchSettings(LinesSearchSettings linesSearchSettings)
        {
            _appConfig.LinesSearchSettings = linesSearchSettings;
            SaveConfig();
        }

        public LinesSearchSettings GetLinesSearchSettings()
        {
            return _appConfig.LinesSearchSettings ?? new LinesSearchSettings();
        }


        private void SaveConfig()
        {
            var fileInfo = new FileInfo(FilesConst.APP_CONFIG_FILE);
            var stream = !fileInfo.Exists ? fileInfo.Create() : new FileStream(fileInfo.FullName, FileMode.Truncate);

            var bf = new BinaryFormatter();

            bf.Serialize(stream, _appConfig);

            stream.Close();
        }
    }
}